﻿Imports System.Data.SqlClient
Public Class clsServicios
    Private mintIdServicio As Integer
    Private mstrNombre As String
    Private mstrDescripcion As String
    Private mdblFrecuencia As Double
    Private mintStatus As Integer
    Private mdblCosto As Double
    Public Property IdServicio() As Integer
        Get
            Return mintIdServicio
        End Get
        Set(ByVal value As Integer)
            mintIdServicio = value
        End Set
    End Property
    Public Property Nombre() As String
        Get
            Return mstrNombre
        End Get
        Set(value As String)
            mstrNombre = value
        End Set
    End Property
    Public Property Descripcion() As String
        Get
            Return mstrDescripcion
        End Get
        Set(ByVal value As String)
            mstrDescripcion = value
        End Set
    End Property
    Public Property Frecuencia() As Double
        Get
            Return mdblFrecuencia
        End Get
        Set(ByVal value As Double)
            mdblFrecuencia = value
        End Set
    End Property
    Public Property Status() As Integer
        Get
            Return mintStatus
        End Get
        Set(ByVal value As Integer)
            mintStatus = value
        End Set
    End Property
    Public Property Costo() As Double
        Get
            Return mdblCosto
        End Get
        Set(ByVal value As Double)
            mdblCosto = value
        End Set
    End Property
    Public Sub New()
        mintIdServicio = 0
        mstrNombre = ""
        mstrDescripcion = ""
        mdblFrecuencia = 0
        mintStatus = 0
        mdblCosto = 0
    End Sub
    Public Sub Buscar()
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "SELECT * From Servicios " & _
        " WHERE Nombre='" & mstrNombre & "'"

        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        If rdBuscar.Read() Then
            mstrDescripcion = rdBuscar("Descripcion").ToString.Trim
            mdblFrecuencia = rdBuscar("Frecuencia")
            mintStatus = rdBuscar("Status")
            mdblCosto = rdBuscar("Costo")
        Else
            mstrDescripcion = ""
            mdblFrecuencia = 0
            mintStatus = 0
            mdblCosto = 0
        End If

        rdBuscar.Close()
        cnConn.Close()


    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into Servicios (" & _
                            "Nombre," & _
                            "Descripcion," & _
                            "Frecuencia," & _
                            "Status," & _
                            "Costo) " & _
                            "VALUES (" & _
                            "substring(@Nombre,1,100)," & _
                            "substring(@Descripcion,1,300)," & _
                            "@Frecuencia," & _
                            "@Status," & _
                            "@Costo) "


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("Descripcion", mstrDescripcion)
        Command.Parameters.AddWithValue("Frecuencia", mdblFrecuencia)
        Command.Parameters.AddWithValue("Status", mintStatus)
        Command.Parameters.AddWithValue("Costo", mdblCosto)
        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el servicio ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub Actualizar()

        Dim strSqlQry As String
        Dim cnConn As New SqlConnection

        cnConn.ConnectionString = CitraConnection
        strSqlQry = "Update Servicios Set " & _
                    "Descripcion=substring(@Descripcion,1,300)," & _
                    "Frecuencia=@Frecuencia," & _
                    "Status=@Status," & _
                    "Costo=@Costo " & _
                    "Where Nombre=@Nombre"


        Dim Command As New SqlCommand(strSqlQry, cnConn)

        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("Descripcion", mstrDescripcion)
        Command.Parameters.AddWithValue("Frecuencia", mdblFrecuencia)
        Command.Parameters.AddWithValue("Status", mintStatus)
        Command.Parameters.AddWithValue("Costo", mdblCosto)
        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar el servicio ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If



    End Sub
End Class
